//
//  SC_Search_UserCell.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/4/26.
//

import UIKit
import SnapKit
import Kingfisher

class SC_Search_UserCell: APBaseTableViewCell {
    private var bgView: UIView!
    private var avatarImageView: UIImageView!
    private var nicknameLabel: UILabel!
    private var idLabel: UILabel!
    private var fansCountLabel: UILabel!

    override func initUI() {
        super.initUI()
        
        contentView.backgroundColor = .clear
        bgView = UIView().then({ make in
            contentView.addSubview(make)
            make.layer.cornerRadius = 10
            make.layer.masksToBounds = true
        })
        
        avatarImageView = UIImageView().then({ make in
            bgView.addSubview(make)
            make.contentMode = .scaleAspectFill
        })
        
        nicknameLabel = UILabel().then({ make in
            bgView.addSubview(make)
            make.font(.ap.medium(14)).textColor(.ap.hex("191702"))
        })
        
        idLabel = UILabel().then({ (make) in
            bgView.addSubview(make)
            make.font(.ap.regular(12)).textColor(.ap.hex("7D7D85"))
        })
        
        fansCountLabel = UILabel().then({ (make) in
            bgView.addSubview(make)
            make.font(.ap.regular(12)).textColor(.ap.hex("7D7D85"))
        })
    }
    
    override func makeConstraints() {
        super.makeConstraints()
        bgView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.bottom.equalTo(-10)
            make.top.equalToSuperview()
        }
        
        avatarImageView.snp.makeConstraints { make in
            make.size.equalTo(68)
            make.centerY.equalToSuperview()
            make.left.equalTo(11)
        }
        
        nicknameLabel.snp.makeConstraints { make in
            make.top.equalTo(avatarImageView)
            make.left.equalTo(avatarImageView.snp.right).offset(8)
            make.width.lessThanOrEqualTo(180)
        }
        
        idLabel.snp.makeConstraints { make in
            make.top.equalTo(avatarImageView).offset(37)
            make.left.equalTo(avatarImageView.snp.right).offset(8)
        }
        
        fansCountLabel.snp.makeConstraints { make in
            make.left.equalTo(idLabel)
            make.bottom.equalTo(avatarImageView)
        }
    }
    
    override func setup(_ model: Any?) {
        guard let model = model as? SC_Search_UserModel else { return }
        if let avatar = model.avatar, !avatar.isEmpty {
            avatarImageView.kf.setImage(with: URL(string: avatar))
        }
        nicknameLabel.text = model.nickname
        idLabel.text = "id: \(model.id)"
        
        if model.fans_cnt == 0 {
            fansCountLabel.isHidden = true
        } else {
            fansCountLabel.text = "\(model.fans_cnt)粉丝"
            fansCountLabel.isHidden = false
        }
    }
}

extension SC_Search_UserCell: APView_Height_Protocol {
    static func viewHeight(with model: Any?) -> CGFloat {
        return 90
    }
}
